package com.democode.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demoCode.common.common.BaseResponse;
import com.demoCode.common.common.ErrorCode;
import com.demoCode.common.common.ResultUtils;
import com.demoCode.common.exception.BusinessException;
import com.demoCode.model.auth.dto.AuthLoginDto;
import com.demoCode.model.auth.dto.AuthRegisterDto;
import com.demoCode.model.auth.vo.AuthLoginVo;
import com.demoCode.model.user.dto.PageUserDto;
import com.demoCode.model.user.dto.UpdateUserDto;
import com.demoCode.model.user.vo.UserVo;
import com.democode.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     * @param authLoginDto
     * @return 用户登录信息
     */
    @PostMapping("/login")
    public AuthLoginVo userLogin(@RequestBody AuthLoginDto authLoginDto) {
        if(authLoginDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }
        return userService.userLogin(authLoginDto);
    }

    /**
     * 用户注册
     * @param authRegisterDto
     * @return 布尔值
     */
    @PostMapping("/register")
    public Boolean userRegister(@RequestBody AuthRegisterDto authRegisterDto) {
        if(authRegisterDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }
        return userService.userRegister(authRegisterDto);
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public Boolean userLogout() {
        return userService.userLogout();
    }

    /**
     * 分页查询用户列表
     * @param pageUserDto
     * @return 用户列表
     */
    @PostMapping("/page")
    public BaseResponse<IPage<UserVo>> pageUser(@RequestBody PageUserDto pageUserDto) {
        if(pageUserDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }
        IPage<UserVo> userVos = userService.pageUser(pageUserDto);
        return ResultUtils.success(userVos);
    }

    /**
     * 更新用户信息
     * @param updateUserDto
     * @return
     */
    @PostMapping("/update")
    public Boolean updateUser(@RequestBody UpdateUserDto updateUserDto) {
        if(updateUserDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }

        return userService.updateUser(updateUserDto);
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Boolean deleteUser(@RequestBody List<String> ids) {
        if(ids == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }
        return userService.removeUser(ids);
    }

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public UserVo getUser(@PathVariable Long id) {
        if(id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数不能为空");
        }
        return userService.getUserById(id);
    }

}
